package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.expression.spel.ast.NullLiteral;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartControlle shoppingCartControlle;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据:{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 后台订单详情
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,Long number, String beginTime, String endTime){
        Page<Orders> orderPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(number != null,Orders::getNumber,number);
        lqw.between(beginTime != null, Orders::getCheckoutTime,beginTime,endTime);
        orderService.page(orderPage,lqw);
        return R.success(orderPage);
    }

    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> uptade(@RequestBody Orders orders){
        orderService.updateById(orders);
        return R.success("修改成功");
    }

    @GetMapping("/userPage")
    public R<Page> userPage(Integer page,Integer pageSize){
        Page<Orders> userPage = new Page<>(page,pageSize);
        Long id = BaseContext.getCurrentId();
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();

        lqw.eq(id != null,Orders::getUserId,id);
        lqw.orderByDesc(Orders::getOrderTime);
        orderService.page(userPage,lqw);

        List<OrdersDto> ordersDtos = userPage.getRecords().stream().map((item)->{
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(userPage,ordersDto,"records");

            LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(item.getId()!= null,OrderDetail::getOrderId,item.getId());
            List<OrderDetail> detailList = orderDetailService.list(wrapper);
            ordersDto.setOrderDetails(detailList);

            BeanUtils.copyProperties(item,ordersDto);
            return ordersDto;
        }).collect(Collectors.toList());
        Page<OrdersDto> ordersDtoPage = new Page<>();

        ordersDtoPage.setRecords(ordersDtos);
        return R.success(ordersDtoPage);

    }

    /**
     * 再来一单
     * 1.根据传回来的订单Id 查询订单详细表
     * 2.将订单详细表再次添加到购物车
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders){
        //1.根据传回来的订单Id 查询订单详细表

        LambdaQueryWrapper<OrderDetail> orderDeteilLqw = new LambdaQueryWrapper<>();
        orderDeteilLqw.eq(orders.getId() != null, OrderDetail::getOrderId, orders.getId());

        //调用方法查询 返回菜品集合
        List<OrderDetail> orderlist = orderDetailService.list(orderDeteilLqw);

        //2.将订单详细表再次添加到购物车  创建购物车对象
        List<ShoppingCart> shopList =orderlist.stream().map((orderItem) ->{

            //创建购物车对象
            ShoppingCart shoppingCart = new ShoppingCart();

            shoppingCart.setName(BaseContext.getCurrentId().toString());        //设置名称
            shoppingCart.setImage(orderItem.getImage());               //设置图片
            shoppingCart.setNumber(orderItem.getNumber());             //设置数量
            shoppingCart.setDishId(orderItem.getDishId());             //设置菜品ID
            shoppingCart.setDishFlavor(orderItem.getDishFlavor());     //设置菜品口味
            shoppingCart.setCreateTime(LocalDateTime.now());           //设置创建时间
            shoppingCart.setAmount(orderItem.getAmount());             //设置金额
            shoppingCart.setSetmealId(orderItem.getSetmealId());       //设置套餐 Id


            return shoppingCart;
        }).collect(Collectors.toList());

        for (ShoppingCart shoppingCart : shopList) {
            shoppingCartControlle.add(shoppingCart);
        }

        return R.success("添加成功");
    }



}